package com.cheyun.netsalev3.view.dm.detail

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.RecyclerView
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyPagedListAdapter
import com.cheyun.netsalev3.bean.dm.DmClueList
import com.cheyun.netsalev3.databinding.ActivityDmSearchResultBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.api.NetworkState

class DmCallHisgoryActivity : BaseActivity<ActivityDmSearchResultBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_dm_search_result
    }

    var hashMap = HashMap<String, String>()
    override fun initViewModel(): ViewModel? {
//        var json = JSONObject()
//        var planType = 1
//        var searchKey = ""
//        intent.getIntExtra("planType",1)?.apply {
//            planType = this
//        }
//        intent.getStringExtra("searchKey")?.apply {
//            searchKey = this
//        }
//        if (planType!=0) {
//            json.put("planType",planType)
//            if(planType<4)
//                json.put("dealed",0)
//            else
//                json.put("dealed",1)
//        }
//        json.put("searchKey",searchKey);
//        val factory = InjectorUtils.getDmListViewModelFactory(json.toString())
//        return ViewModelProviders.of(this, factory).get(DmListViewModel::class.java)
        return null
    }

    private lateinit var adapter: MyPagedListAdapter<DmClueList>
    override fun initView() {
        adapter = MyPagedListAdapter(R.layout.fragment_dm_item, binding.viewModel)
        var recyclerView = binding.root.findViewById<RecyclerView>(R.id.swipe_target)
        recyclerView.adapter = adapter
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.viewModel?.refresh()
        }
    }

    override fun initData() {
        binding.viewModel?.items?.observe(this, Observer { list ->
            list?.apply {
                adapter.submitList(list)
            }
        })
        binding.viewModel?.networkState?.observe(this, Observer {
            binding.swipeRefreshLayout.isRefreshing = it == NetworkState.LOADING
            adapter.setNetworkState(it)
        })
    }

}
